<template>
  <div class="page">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新增</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              tooltip: '编辑',
              onClick: handleEdit.bind(null, record)
            },
            {
              icon: 'ant-design:delete-outlined',
              tooltip: '删除',
              color: 'error',
              popConfirm: {
                title: '是否删除该条记录？',
                confirm: handleRemove.bind(null, record)
              }
            }
          ]"
        />
      </template>
    </BasicTable>
    <EditDrawer @register="registerEditDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  /**
   * 
   * @date 2025-08-19
   * @author exchenming
   * @Email 276490943@qq.com
   */
  import { message } from 'ant-design-vue'
  import { useTable } from '@/components/Table'
  import { useDrawer } from '@/components/Drawer'
  import EditDrawer from './DetailDrawer.vue'
  import { listDetail, removeDetail } from '@/api/audio/detail'
  import { tableColumns, searchFormSchema } from './detail.data'

  const [registerTable, { reload: reloadList }] = useTable({
    title: '列表',
    titleHelpMessage: '列表',
    api: listDetail,
    rowKey: 'flowno',
    columns: tableColumns,
    useSearchForm: true,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema
    },
    showIndexColumn: true,
    actionColumn: {
      width: 120
    }
  })

  const [registerEditDrawer, { openDrawer: openEditDrawer }] = useDrawer()

  const handleCreate = () => {
    openEditDrawer(true, { isUpdate: false })
  }

  const handleEdit = (record: any) => {
    openEditDrawer(true, { record, isUpdate: true })
  }

  const handleRemove = ({ flowno }) => {
    removeDetail({ flowno }).then((res) => {
      if (res.code === 0) {
        message.success('删除成功！')
        reloadList()
      }
    })
  }

  const handleSuccess = () => {
    reloadList()
  }
</script>